package com.mxx.common.data.jpa;

import com.mxx.common.core.SFunction;
import com.mxx.common.utils.SFunctionUtil;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import java.util.ArrayList;

/**
 * @author: mxx
 * @date: 2021/8/12
 * @time: 9:46
 * @Description: 描述信息
 */
public class LambdaSpecHelper<T> extends AbstractSpecHelper<LambdaSpecHelper<T>, SFunction<T, ?>> {


    public LambdaSpecHelper(Root root, CriteriaBuilder criteriaBuilder, CriteriaQuery criteriaQuery) {
        super(root, criteriaBuilder, criteriaQuery);
        this.root = root;
        this.criteriaBuilder = criteriaBuilder;
        this.predicateList = new ArrayList<>();
        this.criteriaQuery = criteriaQuery;
    }

    @Override
    protected Path get(SFunction<T, ?> key) {
        String fieldName = SFunctionUtil.getFieldName(key);
        try {
            return root.get(fieldName);
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex.getMessage());
        }
    }


    @Override
    public LambdaSpecHelper<T> getTarget() {
        return this;
    }
}
